import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../constants';
import TextInput from 'Shared/components/TextInput';
import TagSelect from 'Shared/components/TagSelect';
import {PERMISSION} from './constants';
import routes from './routes';
import WrappedSelect from '../common/WrappedSelect';
import {injectIntl} from './intl';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import TextArea from 'Shared/components/TextArea';
import DateRangePicker from 'Shared/components/DateRangePicker';

const FormItem = Form.Item;

class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    onSelectDealer = data => {
        this.props.onConditionsChange('dealerCode', data.code);
        this.props.onQueryDealerChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }

    render() {
        const {formatMessage} = this.props.intl;
        const {initEnum, queryDealer, conditions} = this.props;
        const {id, code, name} = queryDealer;
        const dealerInfo = conditions.dealerCode ? {
            id,
            code,
            name
        } : null;
        return (
            <Form className="form-standard">
                <Row>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={formatMessage({
                                id: 'queryPanel.form.dealerId',
                                defaultMessage: '总代'
                            })}
                            {...FORM_OPTIONS.item}>
                            <DealerSelectPanel
                                value={dealerInfo}
                                onSelect={this.onSelectDealer} />
                        </FormItem>
                    </Col>
                    {initEnum.brands.length !== 1 && <Col {...FORM_OPTIONS.col}><FormItem label={formatMessage({
                        id: 'queryPanel.form.brandId',
                        defaultMessage: '品牌'
                    })}{...FORM_OPTIONS.item}>
                        <WrappedSelect
                            allowClear
                            name="brandCode"
                            value={conditions.brandCode}
                            options={initEnum.brands}
                            onChange={this.handleFilterChange}/>
                    </FormItem>
                    </Col>}
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={formatMessage({
                                id: 'queryPanel.form.codes',
                                defaultMessage: '单据号'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextArea
                                name="codes"
                                placeholder={formatMessage({
                                    id: 'queryPanel.form.codes.placeholder',
                                    defaultMessage: '最多输入1000个，以回车隔开'
                                })}
                                value={conditions.codes}
                                onBlur={this.handleFilterChange}/>
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'queryPanel.form.confirmerName',
                            defaultMessage: '操作人姓名'
                        })}{...FORM_OPTIONS.item}>
                            <TextInput
                                name="confirmerName"
                                value={conditions.confirmerName}
                                onPressEnter={this.handleFilterPressEnter}
                                onChange={this.handleFilterChange}/>
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={formatMessage({
                                id: 'queryPanel.form.confirmTime',
                                defaultMessage: '操作日期'
                            })}
                            {...FORM_OPTIONS.item}>
                            <DateRangePicker name="confirmTime" value={conditions.confirmTime}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col>
                        <FormItem label={formatMessage({
                            id: 'queryPanel.form.prtDataTypes',
                            defaultMessage: '单据类型'
                        })} {...FORM_ROW_OPTIONS.item}>
                            <TagSelect
                                name="prtDataTypes"
                                value={conditions.prtDataTypes}
                                options={initEnum.partDataTypes}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={6}>
                        <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>{formatMessage({
                            id: 'queryPanel.btn.query',
                            defaultMessage: '查询'
                        })}</Button>
                        <Button key="clear" onClick={this.props.onResetClick}>{formatMessage({
                            id: 'queryPanel.btn.reset',
                            defaultMessage: '重置'
                        })}</Button>
                    </Col>
                    <Col span={18} className="col-align-right">
                        {this.props.deliveryOrderable &&
                        <Button key="deliveryOrder" type="primary" href={`#${routes.deliveryOrder.url()}`}>{formatMessage({
                            id: 'queryPanel.btn.deliveryOrder',
                            defaultMessage: '交货单状态修改'
                        })}</Button>}
                        {this.props.partSaleOrderrable &&
                        <Button key="partSaleOrder" type="primary" href={`#${routes.partSaleOrder.url()}`}>{formatMessage({
                            id: 'queryPanel.btn.partSaleOrder',
                            defaultMessage: '销售订单状态修改'
                        })}</Button>}
                    </Col>
                </Row>
            </Form>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onQueryDealerChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    deliveryOrderable: PropTypes.bool,
    importable: PropTypes.bool,
    initEnum: PropTypes.object,
    intl: PropTypes.object,
    partSaleOrderrable: PropTypes.bool,
    queryDealer: PropTypes.object,
};

import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, saveQueryDealer} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'initData']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.code
        })) : [];
        const partDataTypes = initData.partDataTypes ? initData.partDataTypes.map(item => ({
            text: item.name,
            value: item.id
        })) : [];
        return {
            brands,
            partDataTypes
        };
    }
);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    deliveryOrderable: state.getIn(['page', 'domainData', 'permission', 'data']).includes(PERMISSION.deliveryOrder),
    partSaleOrderrable: state.getIn(['page', 'domainData', 'permission', 'data']).includes(PERMISSION.partSaleOrder),
    initEnum: getinitEnumData(state),
    queryDealer: getQueryDealer(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onQueryDealerChange: data => dispatch(saveQueryDealer(data)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
